﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SviluppoDistinta.Library;

namespace SviluppoDistinta.WinApp
{
    public partial class NumeroCapi : Form
    {
        private Ordine ordine;
        public NumeroCapi(Ordine ordine)
        {
            InitializeComponent();
            this.ordine = ordine;
        }

        private void NumeroCapi_Load(object sender, EventArgs e)
        {
            try
            {
                grdNumeroCapi.Columns.Add(new DataGridViewTextBoxColumn() { HeaderText = "Colore" });
                foreach (Taglia t in ordine.Taglie)
                {
                    DataGridViewTextBoxColumn col = new DataGridViewTextBoxColumn();
                    col.HeaderText = t.Nome;
                    col.Tag = t;
                    grdNumeroCapi.Columns.Add(col);
                }

                grdNumeroCapi.Columns[0].Width = 88;
                for (int cln = 1; cln < grdNumeroCapi.ColumnCount; cln++)
                {
                    grdNumeroCapi.Columns[cln].Width = 46;
                }

                foreach (Colore c in ordine.Colori)
                {
                    int index = grdNumeroCapi.Rows.Add();
                    grdNumeroCapi.Rows[index].Tag = c;
                }

                foreach (DataGridViewRow row in grdNumeroCapi.Rows)
                {
                    Colore c = (Colore)row.Tag;
                    if (c == null) break;
                    foreach (DataGridViewCell cella in row.Cells)
                    {
                        if (cella.ColumnIndex == 0)
                        {
                            cella.Value = c.Nome;
                        }
                        else
                        {
                            Taglia t = (Taglia)grdNumeroCapi.Columns[cella.ColumnIndex].Tag;
                            cella.Value = ordine.GetNumeroCapi(c, t);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void grdNumeroCapi_CellEndEdit(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                Taglia t = (Taglia)grdNumeroCapi.Columns[e.ColumnIndex].Tag;
                Colore c = (Colore)grdNumeroCapi.Rows[e.RowIndex].Tag;
                if (c != null && t != null)
                {
                    ordine.SetNumeroCapi(c, t, Convert.ToInt32(grdNumeroCapi.Rows[e.RowIndex].Cells[e.ColumnIndex].Value));
                }
                grdNumeroCapi.Columns[2].Width = 40;
                grdNumeroCapi.Columns[3].Width = 80;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
            
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            this.Close();
        }

    }
}
